package com.baiku.sanhos.controller;

import com.baiku.sanhos.mapper.NoticeMapper;
import com.baiku.sanhos.pojo.dto.NoticeDTO;
import com.baiku.sanhos.pojo.entity.Doctor;
import com.baiku.sanhos.pojo.entity.Notice;
import com.baiku.sanhos.pojo.vo.DoctorIndexVo;
import com.baiku.sanhos.pojo.vo.NoticeBgIndexVo;
import com.baiku.sanhos.pojo.vo.NoticeIndexVo;
import com.baiku.sanhos.response.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
@Api(tags = "通知管理")
@RestController
@RequestMapping("/pa/notice")
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 新增公告信息
     * @param notice
     * @return
     */
    @ApiOperation("新增公告信息")
    @PostMapping("/addNotice")
    public ResultVO addNotice(@RequestBody Notice notice){
        noticeMapper.addNotice(notice);
        return ResultVO.ok();
    }

    /**
     * 查询全部公告信息，并且分页与查询
     * @param page
     * @param pageSize
     * @param notice
     * @return
     */
    @ApiOperation("查询全部公告信息，并且分页与查询")
    @GetMapping("/selectAll")
    public ResultVO selectAll(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("notice") String notice) {
        Map<String, Object> map = new HashMap<>();
        int total = noticeMapper.getTotalNotices(notice);//总条数
        int totalPages = (int)Math.ceil((double)total/pageSize);//计算总页数
        List<NoticeBgIndexVo> notices;
        if(totalPages==0){
            notices = new ArrayList<>();
        }else {
            int offset = (page - 1) * pageSize;
            notices = noticeMapper.selectAll(pageSize,offset,notice);
        }
        map.put("notices", notices);
        map.put("total", total);
        return ResultVO.ok(map);
    }

    /**
     * 根据id删除公告信息
     * @param id
     * @return
     */
    @ApiOperation("根据id删除公告信息")
    @DeleteMapping ("/deleteNoticeId/{id}")
    public ResultVO deleteNoticeId(@PathVariable Integer id){
        noticeMapper.deleteNoticeId(id);
        return ResultVO.ok();
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @ApiOperation("根据id批量删除")
    @DeleteMapping("/deleteByIds")
    public ResultVO deleteByIds(@RequestBody List<Integer> ids){
        noticeMapper.deleteNoticeByIds(ids);
        return ResultVO.ok();
    }
    /**
     * 获取公告内容与公告主题
     * @return
     */
    @ApiOperation("获取公告内容与公告主题")
    @GetMapping("/selectOne")
    public ResultVO selectOne(){
        List<Notice> notices  = noticeMapper.selectOne();
        return ResultVO.ok(notices);
    }

    /**
     * 获取医院挂号须知标题与内容
     *
     */
    @ApiOperation("获取医院挂号须知标题与内容")
    @GetMapping("/selectTwo")
    public ResultVO selectTwo(){
        List<Notice> notices  = noticeMapper.selectTwo();
        return ResultVO.ok(notices);
    }

    /**
     * 更新公告主题
     * @param noticeDTO
     * @return
     */
    @ApiOperation("更新公告主题")
    @PutMapping("/updateById")
    public ResultVO updateById(@RequestBody NoticeDTO noticeDTO){
        noticeMapper.updateById(noticeDTO);
        return ResultVO.ok();
    }
    @ApiOperation("根据时间字段查询最新两条数据")
    @GetMapping("/selectIndexAll")
    public ResultVO selectIndexAll(){
        List<Notice> notices = noticeMapper.selectIndexAll();
        // 根据时间字段进行排序
      //  notices.sort(Comparator.comparing(Notice::getIssueTime).reversed());
        // 只保留前两条记录
        if (notices.size() > 2) {
            notices = notices.subList(0, 2);
        }
        return ResultVO.ok(notices);
    }
}
